import Cesium, { GeometryInstance, Primitive } from "../Ces/Cesium"


class WeWindStreamlinePrimitive {
  private primitive: Primitive
  constructor({
    features,
    scaleZ,
    width,
    outlineColor,
    outlineWidth,
    extrusionHeight
  }) {
    const geometryInstances: GeometryInstance[] = []

    for (const f of features) {
      const positions: any[] = []
      for (let i = 0; i < f.geometry.coordinates.length; i += 3) {
        const lon = f.geometry.coordinates[i]
        const lat = f.geometry.coordinates[i + 1]
        const height = f.geometry.coordinates[i + 2]
        positions.push(Cesium.Cartesian3.fromDegrees(lon, lat, height * extrusionHeight * scaleZ))
      }
      const geometry = new Cesium.PolylineGeometry({
        positions,
        vertexFormat: Cesium.PolylineMaterialAppearance.VERTEX_FORMAT,
        width,
      })
      geometryInstances.push(
        new Cesium.GeometryInstance({
          geometry,
          attributes: {
            color: Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.DARKCYAN),
          },
        }),
      )
    }

    const primitive = new Cesium.Primitive({
      geometryInstances,
      appearance: new Cesium.PolylineMaterialAppearance({
        translucent: false,
        material: Cesium.Material.fromType(Cesium.Material.PolylineOutlineType),
      }),
      asynchronous: false,
    })

    primitive.appearance.material.uniforms.outlineColor = outlineColor
    primitive.appearance.material.uniforms.outlineWidth = outlineWidth

    this.primitive = primitive
  }

  update(frameState) {
    //@ts-ignore
    this.primitive?.update(frameState)
  }

  isDestroyed() { return false }

  destroy() {
    //@ts-ignore
    this.primitive = this.primitive && this.primitive.destroy()
    return Cesium.destroyObject(this)
  }
}

export default WeWindStreamlinePrimitive
